<?php
#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------


#+------------------------------------------------------------------
#| 普通的。
#+------------------------------------------------------------------
#| Author:Janmas Cromwell <janmas-cromwell@outlook.com>
#+------------------------------------------------------------------

include __DIR__ . '/vendor/autoload.php';

use app\library\websocket\logic\Friends;
use app\library\websocket\logic\Sessions as Chat;
use app\library\websocket\logic\Users as User;
use encryption\Encryption;

$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();

$tokenMap = [
    123 => Encryption::encrypt(1),
    456 => Encryption::encrypt(2),
    789 => Encryption::encrypt(3)
];

foreach ($tokenMap as $key => $value) {
    $redis->set($key, $value);
}

$userMap = [
    123 => [
        'user_id'  => 1,
        'identity' => Encryption::encrypt(1),
        'username' => 'Janmas',
        'avatar'   => 'https://moyuu.gitee.io/image/avatar.jpeg',
    ],
    456 => [
        'user_id'  => 2,
        'identity' => Encryption::encrypt(2),
        'username' => 'Moyuu',
        'avatar'   => 'https://portrait.gitee.com/uploads/avatars/user/708/2125438_moyuu_1614151579.png!avatar60',
    ],
    789 => [
        'user_id'  => 3,
        'identity' => Encryption::encrypt(3),
        'username' => 'Jm',
        'avatar'   => 'https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eqACMnVYWDXvoVKkNzpZsW2dtTjddgwiaqoA3YJE9KjS8bwggfgC4q9YA35iaY8zT712uLv48bL8JVg/132',
    ]
];
foreach ($userMap as $token => $user) {
    $hashKey = User::PREFIX . $user['identity'];
    foreach ($user as $key => $value) {
        $redis->hSet($hashKey, $key, $value);
    }
}
$friendShipMap = [
    Encryption::encrypt(1) => [
        Encryption::encrypt(2), Encryption::encrypt(3)
    ],
    Encryption::encrypt(2) => [
        Encryption::encrypt(1),
        Encryption::encrypt(3)
    ],
    Encryption::encrypt(3) => [
        Encryption::encrypt(1),
        Encryption::encrypt(2)
    ],
];

foreach ($friendShipMap as $userId => $ship) {
    foreach ($ship as $value) {
        $redis->sAdd((Friends::PREFIX) . Encryption::encrypt($userId), $value);
    }
}

$chatGroupMap = [
    'qwe' => [
        'client'       => Encryption::encrypt(1),
        'touid'        => Encryption::encrypt(2),
        'last_time'    => time(),
        'last_message' => '明天见',
        'user_del'     => 0,
        'to_del'       => 0,
        'chat_id'      => 'qwe',
        'user_read'    => 0,
        'to_read'      => 0,
        'group_id'     => 0
    ],
    'asd' => [
        'client'       => Encryption::encrypt(1),
        'touid'        => Encryption::encrypt(3),
        'last_time'    => time(),
        'last_message' => '你觉得2号怎么样,靠谱吗？',
        'user_del'     => 0,
        'to_del'       => 0,
        'chat_id'      => 'asd',
        'user_read'    => 0,
        'to_read'      => 0,
        'group_id'     => 0
    ],
    'zxc' => [
        'client'       => Encryption::encrypt(2),
        'touid'        => Encryption::encrypt(3),
        'last_time'    => time(),
        'last_message' => '老哥最近有哥项目你有兴趣嘛',
        'user_del'     => 0,
        'to_del'       => 0,
        'chat_id'      => 'zxc',
        'user_read'    => 0,
        'to_read'      => 0,
        'group_id'     => 0
    ]
];
foreach ($chatGroupMap as $chatId => $value) {
    foreach ($value as $key => $val) {
        $redis->hSet($chatId, $key, $val);
    }
    $userChatKey = Chat::PREFIX . ($value['client']);
    $touidChatKey = Chat::PREFIX . ($value['touid']);
    $redis->sAdd($userChatKey, $chatId);
    $redis->sAdd($touidChatKey, $chatId);
}

$messages = [
    111 => [
        'msg_id'    => 111,
        'to'        => Encryption::encrypt(1),
        'from'      => Encryption::encrypt(2),
        'timestamp' => time(),
        'data'      => '你好啊，最近怎么样？[微笑]',
        'link'      => '',
        'action'    => 'text',
        'is_read'   => 0,
        'status'    => 1,
        'user_del'  => 0,
        'to_del'    => 0,
        'is_group'  => 0,
        'recall'    => 0,
        'chat_id'   => 'qwe'
    ],
    222 => [
        'msg_id'    => 222,
        'to'        => Encryption::encrypt(2),
        'from'      => Encryption::encrypt(1),
        'timestamp' => time(),
        'data'      => '多谢关心[抱拳][抱拳]，最近还不错',
        'link'      => '',
        'action'    => 'text',
        'is_read'   => 0,
        'status'    => 1,
        'user_del'  => 0,
        'to_del'    => 0,
        'is_group'  => 0,
        'recall'    => 0,
        'chat_id'   => 'qwe'
    ],
];
$prefix = \app\library\websocket\logic\Messages::PREFIX;
foreach ($messages as $msgId => $message) {
    foreach ($message as $key => $val) {
        $redis->hSet($prefix . $msgId, $key, $val);
    }
    $sessionId = Chat::MSG . $message['chat_id'];
    $redis->sAdd($sessionId, $prefix . $msgId);
    $redis->hSet($message['chat_id'], 'last_time', $message['timestamp']);
    $redis->hSet($message['chat_id'], 'last_message', $message['data']);
}
